package root.electronclass.electronclass.personal.dialog

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.CommonDialogFragment
import root.electronclass.electronclass.common.util.ToastUtil
import root.electronclass.electronclass.R
import root.electronclass.electronclass.mvp.entity.FileData
import root.electronclass.electronclass.mvp.entity.FileDown
import root.electronclass.electronclass.mvp.rest.RestManager
import root.electronclass.electronclass.databinding.DialogSchoolStudyDownBinding
import root.electronclass.electronclass.databinding.ItemSchoolStudyDownDialogBinding
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
 *
 * @author Lokiy
 * @date 2023/2/18 21:10
 */
class SchoolStudyDownDialog : CommonDialogFragment<DialogSchoolStudyDownBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val homeworkItems: ArrayList<FileData> = arguments?.getParcelableArrayList(DATA) ?: return
        binding.update(homeworkItems)


        onOkClick = click@{
            if (ids.size == 0) {
                Toast.makeText(requireActivity(), "请选择需要下载的文件", Toast.LENGTH_SHORT).show()
                return@click
            }
            lifecycleScope.launch(CoroutineExceptionHandler()) {
                withContext(Dispatchers.IO) {
                    RestManager.getInstance().initRest().download(FileDown(ids))
                }
                ToastUtil.showToast(requireContext(), "下载成功")
                onSuccess.invoke()
                dismiss()

            }
        }

        onCancelClick = {
            dismiss()
        }
    }

    companion object {
        const val DATA = "_DATA"
    }

}

var onSuccess :() -> Unit = {}

fun DialogSchoolStudyDownBinding.update(it: ArrayList<FileData>) {
    Log.i("SchoolStudyDownDialog", "update: $it")
    ids.clear()
    recycler.adapter = adapter
    adapter.notifyChanged(it)

}

private var ids = ArrayList<String>()

private val adapter = BasicAdapter<FileData, ItemSchoolStudyDownDialogBinding> { holder, item ->
    val index: Int = item.dataUrl.lastIndexOf("/")
    holder.binding.name.text = item.dataUrl.substring(index + 1)
    holder.binding.check.setBackgroundResource(if (item.check) R.drawable.check_true else R.drawable.check_false)
    holder.binding.file.setBackgroundResource(if (item.check) R.drawable.file_true else R.drawable.file_false)
    holder.binding.item.setBackgroundResource(if (item.check) R.drawable.school_study_down_item_bg else R.drawable.school_study_down_item_bg_false)
    holder.binding.item.setOnClickListener {
        item.check = !item.check
        notifyItemChanged(holder.adapterPosition)
        if (item.check) {
            ids.add(item.id)
        } else {
            ids.remove(item.id)
        }
    }
}



